//
//  ShareView.m
//  CarTown
//
//  Created by xionghao on 15/8/10.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "ShareView.h"
#import "ShareButton.h"
#import "UMSocialWechatHandler.h"
#import "WXApi.h"
@interface ShareView()
{
    /** 所有按钮 */
    NSArray *_buttonTitles;
    
    /** 暗黑色的view */
    UIView *_darkView;
    
    /** 所有按钮的底部view */
    UIView *_bottomView;
    
    UIButton *_cancel;

}
@property (nonatomic,strong) UIWindow *backWindow;
@end

@implementation ShareView
-(id)initWithTitle:(NSArray *)titlearray andIcons:(NSArray *)icons delegate:(id<ShareclickDelegate>) delegate{
    if (self=[super init]) {
        _delegate=delegate;
        [self setupbutton:titlearray icon:icons];
    }
    return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)setupbutton:(NSArray *)titlearray icon:(NSArray *)icons{
    // 暗黑色的view
    UIView *darkView = [[UIView alloc] init];
    [darkView setAlpha:0];
    [darkView setUserInteractionEnabled:NO];
    [darkView setFrame:CGRectMake(0, 0, XHScreenwidth, XHScreenheight)];
    [darkView setBackgroundColor:UIColorWithRGB(46, 49, 50)];
    [self addSubview:darkView];
    _darkView = darkView;
    [self addSubview:darkView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
    [darkView addGestureRecognizer:tap];
    
    
    UIView *bottomview=[[UIView alloc]init];
    bottomview.width=XHScreenwidth;
    bottomview.x=0;
    bottomview.backgroundColor=UIColorWithRGB(245, 245, 245);
    bottomview.height=140;
    bottomview.y=XHScreenheight;
    _bottomView=bottomview;
     BOOL isinstalled=[WXApi isWXAppInstalled];
    [self addSubview:bottomview];
    UIButton *cancel=[[UIButton alloc]initWithFrame:CGRectMake(0, _bottomView.height-44, XHScreenwidth, 44)];
    cancel.titleLabel.font=[UIFont systemFontOfSize:14];
    [cancel setTitle:@"取消" forState:UIControlStateNormal];
    [cancel setTitleColor:UIColorWithRGB(66, 66, 66) forState:UIControlStateNormal];
    [cancel setBackgroundColor:UIColorWithRGB(255, 255, 255)];
    
    [cancel addTarget:self action:@selector(cancelclick) forControlEvents:UIControlEventTouchUpInside];
    _cancel=cancel;
    _buttonTitles=titlearray;
    [bottomview addSubview:cancel];
    CGFloat bottomwidth=XHScreenwidth/3;
    CGFloat height=bottomview.height-44;
    for (int i=0; i<titlearray.count; i++) {
        ShareButton *button=[[ShareButton alloc]init];
        [button setTitle:titlearray[i] forState:UIControlStateNormal];
        [button setTitleColor:UIColorWithRGB(66, 66, 66) forState:UIControlStateNormal];
        button.titleLabel.font=[UIFont systemFontOfSize:13];
        [button setImage:icons[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(shareclick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag=i;
        button.height=height;
        button.width=bottomwidth;
        button.x=bottomwidth*i;
        button.y=10;
        if (!isinstalled) {
            
            if (i==2) {
                button.x=0;
                [bottomview addSubview:button];
            }
          
        }else{
           [bottomview addSubview:button];
        }
    }
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, XHScreenwidth, 1)];
    view.backgroundColor=UIColorWithRGB(238, 231, 231);
    view.y=bottomview.height-45;
    [bottomview addSubview:view];
    [self setFrame:CGRectMake(0, 0, XHScreenwidth, XHScreenheight)];
    [self.backWindow addSubview:self];
}
- (UIWindow *)backWindow {
    
    if (_backWindow == nil) {
        
        _backWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _backWindow.windowLevel       = UIWindowLevelStatusBar;
        _backWindow.backgroundColor   = [UIColor clearColor];
        _backWindow.hidden = NO;
    }
    
    return _backWindow;
}

- (void)dismiss:(UITapGestureRecognizer *)tap {
    [self dismissAnimi];
}

-(void)dismissAnimi{
    [UIView animateWithDuration:0.3f
                          delay:0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         
                         [_darkView setAlpha:0];
                         [_darkView setUserInteractionEnabled:NO];
                         
                         CGRect frame = _bottomView.frame;
                         frame.origin.y += frame.size.height;
                         [_bottomView setFrame:frame];
                         
                     }
                     completion:^(BOOL finished) {
                         
                         _backWindow.hidden = YES;
                         
                         [self removeFromSuperview];
                     }];

}
- (void)show {
    
    _backWindow.hidden = NO;
    
    [UIView animateWithDuration:0.3f
                          delay:0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         
                         [_darkView setAlpha:0.4f];
                         [_darkView setUserInteractionEnabled:YES];
                         
                         CGRect frame = _bottomView.frame;
                         frame.origin.y -= frame.size.height;
                         [_bottomView setFrame:frame];
                         
                     }
                     completion:nil];
}

-(void)cancelclick{
    [self dismissAnimi];
}
-(void)shareclick:(ShareButton *)sharebtn{
    if ([_delegate respondsToSelector:@selector(shareclickWithIndex:)]) {
        [_delegate shareclickWithIndex:sharebtn.tag];
    }
    [self dismissAnimi];
}
@end
